package com.nuanshui.heatedloan.userinfo.service;

import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.userinfo.dal.mapper.UserStateMapper;
import com.nuanshui.heatedloan.userinfo.dal.mapper.UsersMapper;
import com.nuanshui.heatedloan.userinfo.facade.UserStateService;
import com.nuanshui.heatedloan.userinfo.model.User;
import com.nuanshui.heatedloan.userinfo.model.UserInfo;
import com.nuanshui.heatedloan.userinfo.tool.UserTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by zxh on 2017/2/11.
 */
@Service(ServiceConstant.UserStateService)
@Transactional(readOnly=true,value= TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class UserStateServiceImpl extends CommonServiceImpl implements UserStateService {

    @Autowired
    private UserStateMapper userStateMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserTool userTool;

    @Override
    public Long isTestUser(String sourceUserId)
    {
        return userStateMapper.isTestUser(sourceUserId);
    }

    @Override
    @Transactional(value= TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void updateViewCount(String userId, Long count) {
        userStateMapper.updateViewCount(count, userId);

    }
    @Override
    public Integer userCountByNick(String nick) {
        return userStateMapper.userCountByNick(nick,getCurrentUserId());
    }

    @Override
    public Integer userCountByNick(String nick,String userId)
    {
        return userStateMapper.userCountByNick(nick,userId);
    }

    @Override
    public String getUserChannel(String userId) {
        UserInfo userInfo=usersMapper.loadUserInfo(userId);
        return userInfo.getChannel();
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void setUserChannel(String userId, String channel) {
        userStateMapper.setUserChannel(userId, channel);
    }

    @Override
    public int findNotReadMessage() {
        return userStateMapper.findNotReadMessage(userTool.getCurrentUser().getId());
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void updateNotReadNum(String userId,Integer num) {
        userStateMapper.updateNotReadNum(userId, num);
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public int undelete(String id) {
        return userStateMapper.undeleteByPrimaryKey(id);
    }

    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void activateUser(String id) {
        userStateMapper.activateUser(id);
    }

    /**
     * 点赞用户
     */
    @Override
    @Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
    public void praiseUser(String sourceUserId, String targetUserId, String selfHoodImg) {
        userStateMapper.praiseUser(sourceUserId, targetUserId, selfHoodImg);
    }

    /**
     * 是否点赞过
     */
    @Override
    public Long isPraisedUser(String sourceUserId, String targetUserId, String selfHoodImg) {
        return userStateMapper.isPraisedUser(sourceUserId, targetUserId, selfHoodImg);
    }

    @Override
    public boolean bindPhoneStatus(String userId) {
        User user = usersMapper.selectByPrimaryKey(userId);
        return !SysStringUtils.isEmpty(user.getPhone());
    }
}
